##parameters=exitURL=None,assessmentIdentity=None
# Creates a new assessment based on the current model (context) and redirects to it.
from DateTime import DateTime
userProviderService         = context.knowledgeengine_userproviderservice

request  = context.REQUEST
session  = request.SESSION
redirect = request.RESPONSE.redirect
referrer = exitURL or request['HTTP_REFERER']


if not context.meta_type in ['Model']:
    raise "Model not found"
    
model = context
user  = userProviderService.getUser(request["AUTHENTICATED_USER"].getId())
now   = DateTime()

assessmentName = model.Title() + " " + now.strftime("%Y-%m-%d %I:%m")
assessment     = context.assessment_create( assessmentName, model, user, assessmentIdentity=assessmentIdentity )

identity       = assessment.getIdentity()

if "%s" in referrer:
    referrer = referrer % identity
    
    
from Products.KnowledgeEngine.Utils.helpers import urlquote
referrer = urlquote( referrer )

if referrer:
    url = "%s/assess?assessmentIdentity=%s&exitURL=%s" % (model.absolute_url(), identity, referrer)
else:
    url = "%s/assess?assessmentIdentity=%s" % (model.absolute_url(), identity)


return redirect( url )
